from flask import Flask, request, jsonify
import json
import time
import iot_save

app = Flask(__name__)


@app.route("/iot/hello")
def hello():
    return """
        Hello IOT!!!
    """


@app.route('/iot/getjson', methods=['POST'])
def getjson():
    data = request.get_data()
    json_dict = json.loads(data)

    print(json_dict)
    json_str = json.dumps(json_dict)

    iot_save.insert_demo("INSERT INTO evtest.evpayload(payloadtest) VALUES('%s');" % (json_str))

    with open("log.txt", 'a+', encoding='utf-8') as newFile:
        newFile.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " >> " + json_str + '\n')

    return jsonify({"code": "ok", "message": "插入成功！"})


if __name__ == '__main__':
    app.config['JSON_AS_ASCII'] = False
    app.run(host='0.0.0.0', port=8888, debug=True)
